package cn.wolfcode.wms.controllrer;

import cn.wolfcode.wms.domain.StockOutcomeBill;
import cn.wolfcode.wms.page.PageResult;
import cn.wolfcode.wms.qo.StockOutcomeBillQueryObject;
import cn.wolfcode.wms.service.ICilentService;
import cn.wolfcode.wms.service.IDepotService;
import cn.wolfcode.wms.service.IStockOutcomeBillService;
import cn.wolfcode.wms.util.RequiredPermission;
import cn.wolfcode.wms.vo.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/stockoutcomebill")
public class StockOcomeBillController {

    @Autowired
    private IStockOutcomeBillService orderBillService ;

    @Autowired
    private IDepotService depotService;
    @Autowired
    private ICilentService cilentService;

    @RequestMapping("/list")
    @RequiredPermission("出库订单列表")
    public String list(Model model ,@ModelAttribute("qo") StockOutcomeBillQueryObject qo){
        model.addAttribute("clients",cilentService.selectAll());
        model.addAttribute("depots",depotService.selectAll());
        PageResult pageResult = orderBillService.query(qo);

        model.addAttribute("pageResult",pageResult);
        return "stockoutcomebill/list";
    }
    @RequiredPermission("出库订单删除")
    @RequestMapping("/delete")
    @ResponseBody
    public JSONResult delete(Long id){
        try {
            orderBillService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.mark(e.getMessage());
        }

        return  JSONResult.success("删除成功");
    }

    @RequestMapping("/input")
    public String input(Model model,Long id){
        model.addAttribute("depots",depotService.selectAll());
        if(id != null){
            model.addAttribute("stockoutcomebill",orderBillService.selectByPrimaryKey(id));
        }
        return "stockoutcomebill/input";

    }

    @RequiredPermission("出库订单添加或跟新")
    @RequestMapping("/saveorupdate")
    public String  saveorupdate(StockOutcomeBill stockoutcomebill){
        try {
            orderBillService.saveorupdate(stockoutcomebill);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "redirect:/stockoutcomebill/list.do";
    }

    @RequiredPermission("审核采购订单")
    @RequestMapping("/audit")
    @ResponseBody
    public JSONResult autit(Long id){
        orderBillService.audit(id);

        return  JSONResult.success("审核成功");
    }

    @RequiredPermission("查看视图")
    @RequestMapping("/view")
    public String  view(Long id ,Model model) {
        model.addAttribute("orderBill", orderBillService.selectByPrimaryKey(id));


        return "stockoutcomebill/view";
    }

}
